package com.example.woke1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.CheckpaypswActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class ZhcailicaiinActivity extends Activity implements OnClickListener {

	private Intent intent;
	private EditText mEmoney;
	private String money;
	private View mProgress;
	private MyApp application;
	private String user_money;//�в����
	private TextView mTsefmoney;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhcailicaiin);
		
		intent = new Intent();
		application = (MyApp) getApplication();
		intview();
		
	}

	private void intview() {
		findViewById(R.id.avzhcailicaiin_image_back).setOnClickListener(this);
		findViewById(R.id.avzhcailicaiin_text_ok).setOnClickListener(this);
		mTsefmoney = (TextView) findViewById(R.id.avzhcailicaiin_text_zhcaimoney);
		mProgress = findViewById(R.id.avzhcailicaiin_progress);
		mEmoney = (EditText) findViewById(R.id.avzhcailicaiin_edit_money);
		getmywallet();
	}
	private void getmywallet() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(ZhcailicaiinActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "wallet");
		params.put("username", application.getDatas_load().getUser_login());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("钱包", ""+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jsi = jsonObject.getJSONObject("data");
						user_money = jsi.getString("user_money");
						mTsefmoney.setText("可用剩余"+user_money.subSequence(0, user_money.length() - 3)+"元");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//			Toast.makeText(getActivity(), "ʧ�ܣ�", Toast.LENGTH_SHORT).show();
				Log.e("response", "失败"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
//				mBar.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
//				mBar.setVisibility(View.GONE);
				Log.e("response", "完成");
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == 700 && resultCode == 600){
			payintozhcai();
//			Toast.makeText(ZhcailicaiinActivity.this, "ת���ɹ�", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void payintozhcai() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(ZhcailicaiinActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "financial");
		params.put("a", "addHuoqi");
		params.put("userid", application.getDatas_load().getId());
		params.put("money", money);
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("定期", "成功"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						intent.setClass(ZhcailicaiinActivity.this, ZhcailicaisuecssActivity.class);
						intent.putExtra("money", money);
						intent.putExtra("type", "in");
						startActivity(intent);
						finish();
					}else{
						Toast.makeText(ZhcailicaiinActivity.this, "转入失败", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(ZhcailicaiinActivity.this, "服务器未响应", Toast.LENGTH_SHORT).show();
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mProgress.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mProgress.setVisibility(View.GONE);
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.avzhcailicaiin_image_back:
			finish();
			break;
			
		case R.id.avzhcailicaiin_text_ok:
			money = ""+mEmoney.getText().toString().trim();
			if(money.equals("")){
				Toast.makeText(ZhcailicaiinActivity.this, "金额为空", Toast.LENGTH_SHORT).show();
			}else{
				intent.setClass(ZhcailicaiinActivity.this, CheckpaypswActivity.class);
				intent.putExtra("paymoney", money);
				startActivityForResult(intent, 700);
			}
			break;

		default:
			break;
		}
	}
}
